package com.itheima.health.controller;

import com.itheima.health.dto.SetmealDTO;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import com.itheima.health.utils.aliyunoss.AliyunUtils;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.utils.resources.UploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
@Api(tags = "套餐管理开发")
public class SetmealController {


    // springmvc  消息转化器   @ResponseBody  默认 全字段序列化

    @Reference
    private SetmealService setmealService;


    @PostMapping("setmeal/findPage")
    @ApiOperation(value = "分页查询功能", notes = "分页查询")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {

        return new Result(setmealService.findPage(queryPageBean));
    }

    @PostMapping("setmeal/upload")
    @ApiOperation(value = "图片上传", notes = "套餐图片上传")
    public Result upload(@RequestParam("imgFile") MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();//  获取上传文件真实名称
        String uuidfilename = UploadUtils.generateRandonFileName(originalFilename);// 基于真实文件扩展名 生成uuid名称
        //  文件上传oss
        AliyunUtils.uploadMultiPartFileToAliyun(file.getBytes(), uuidfilename);
        //  图片存储 集合 + 单独保存一个过期时间
        RedisUtil.addToSet(RedisConstant.ALL_SETMEAL_PIC_SET, uuidfilename);
        RedisUtil.set(RedisConstant.SINGLE_PIC + uuidfilename, uuidfilename, 24, TimeUnit.HOURS);
        return new Result(uuidfilename);
    }

    @PostMapping("setmeal/add")
    @ApiOperation(value = "添加功能", notes = "添加套餐")
    public Result add(@RequestBody SetmealDTO setmealDTO) {
        return new Result(setmealService.saveUpdate(setmealDTO));

    }

    @GetMapping("setmeal/findCheckGroupinfosBySetmealId/{id}")
    @ApiOperation(value = "查询功能", notes = "根据检查组id查询对应检查项明细")
    public Result findCheckIteminfosByGroupId(@PathVariable("id") int id) {
        return new Result(setmealService.findCheckGroupDetailById(id));
    }


    @DeleteMapping("setmeal/deleteSetmealById/{id}")
    @ApiOperation(value = "删除删除", notes = "物理删除套餐")
    public Result deleteCheckGroupitemById(@PathVariable("id") int id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setIs_delete(1);
        boolean flag = setmealService.updateById(setmeal);
        RedisUtil.delete("setmeal");
        return new Result(flag);
    }
}





